package com.jju.howe.mvp;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.jju.howe.mvp.bean.User;
import com.jju.howe.mvp.presenter.UserLoginPresenter;
import com.jju.howe.mvp.view.IUserLoginView;

public class UserLoginActivity extends ActionBarActivity implements IUserLoginView {

    private android.support.design.widget.TextInputLayout usernameWrapper;
    private android.support.design.widget.TextInputLayout passwordWrapper;
    private android.widget.Button btnlogin;
    private android.widget.Button btnclear;
    private android.widget.ProgressBar progressBar;

    private UserLoginPresenter mUserLoginPresenter = new UserLoginPresenter(this);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);

        initViews();
    }

    private void initViews() {
        this.progressBar = (ProgressBar) findViewById(R.id.progressBar);
        this.btnclear = (Button) findViewById(R.id.btn_clear);
        this.btnlogin = (Button) findViewById(R.id.btn_login);
        this.passwordWrapper = (android.support.design.widget.TextInputLayout) findViewById(R.id
                .passwordWrapper);
        this.usernameWrapper = (android.support.design.widget.TextInputLayout) findViewById(R.id
                .usernameWrapper);

        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideKeyboard();
                mUserLoginPresenter.login();
            }
        });

        btnclear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideKeyboard();
                mUserLoginPresenter.clear();
            }
        });
    }

    @Override
    public String getUserName() {
        return usernameWrapper.getEditText().getText().toString();
    }

    @Override
    public String getPassword() {
        return passwordWrapper.getEditText().getText().toString();
    }

    @Override
    public void clearUserName() {
        usernameWrapper.getEditText().setText("");
        usernameWrapper.getEditText().requestFocus();
    }

    @Override
    public void clearPassword() {
        passwordWrapper.getEditText().setText("");
    }

    @Override
    public void showLoading() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        progressBar.setVisibility(View.INVISIBLE);
    }


    @Override
    public void toMainActivity(User user) {
        Toast.makeText(this, " login success , to MainActivity", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showFailedError() {
        Toast.makeText(this, "login failed", Toast.LENGTH_SHORT).show();
    }


    /**
     * 隐藏虚拟键盘
     */
    private void hideKeyboard() {
        View view = getCurrentFocus();
        if (view != null) {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
                    hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager
                            .HIDE_NOT_ALWAYS);
        }
    }
}
